require 'album'

class Artist
  attr_accessor :name
  attr_reader :albums
  
  @@all = []
  
  def self.all
    return @@all;
  end
  
  def <=>(other)
     self.name.downcase <=> other.name.downcase
  end
    
  def initialize(name)
    @name = name
    @albums = []
  end

  def addAlbum(a)
    @albums << a
  end
  
  def album(alb_name)
    albums.each do |a|
      if(a.name.downcase == alb_name.downcase)
        return a
      end
    end
    
    if(alb_name == "Unknown Album" )
      a = Album.new(@name,alb_name)
      @albums << a
      return a;
    end
    
    Album.all.each do |a|
      if(a.name.downcase == alb_name.downcase)
        @albums << a
        return a
      end
    end

    a = Album.new(@name,alb_name)
    @albums << a
    return a
  end
  
  def addTrack(t)
    alb = (t.album.to_s == "") ? "Unknown Album" : t.album.to_s
    album(alb).addTrack(t)
  end
  
  def self.artist(name)
	newName = name.downcase
    @@all.each do |a|
      if( newName == a.name.downcase )
        return a
      end
    end
	return nil
  end
  
  def self.artistWith(name)
	ret = []
	match = false
	newName = name.downcase
    @@all.each do |a|
      if( newName.index(a.name.downcase) != nil)
        ret << a
		if( newName == a.name.downcase ) 
			match = true
		end
      end
    end
	if( match == false )
		a = Artist.new(name)
		@@all << a
		ret << a
	end
    return ret
  end

end
